from zope.interface import implements
from pydataportability.model.socialgraph.interfaces import IVCard, IVCardAddress

class VCard(object):
    """models a VCard"""
    
    implements(IVCard)
    
    FIELDS = ['fn','nickname','sort-string','url','email','label','tel','geo','photo','bday','logo','sound','title','role','org','category','note','class','key','mailer','uid','rev']
    ADR_FIELDS = ['post-office-box', 'extended-address', 'street-address', 'locality', 'region', 'postal-code', 'country-name', 'type', 'value']
    
    # we need to map some fields to different names because of reserved python words:
    FIELD_PYTHON_MAP = {'class' : 'klass',
                        'type'  : 'vtype'}

    def __init__(self, **kwargs):
        self.data = {}
        self.adr = {}
        for field in self.FIELDS:
            self.data[field] = kwargs.get(self._convertFieldName(field),u'')
            #self[field] = kwargs.get(self._convertFieldName(field),u'')
        for field in self.ADR_FIELDS:
            self.adr[field] = kwargs.get(self._convertFieldName(field),u'')
            
    def _convertFieldName(self,name):
        """convert a fieldname to the one we use in the class to make it python compatible"""
        name = name.replace("-","_")                # no - allowed, use _ instaed
        name = self.FIELD_PYTHON_MAP.get(name,name)      # some names are reserved words to replace them
        return name
    
    def __setitem__(self,name,value):
        """store an item in the dict"""
        if name not in self.FIELDS:
            raise KeyError, "'%s' is not a valid key, please use one of %s" %(name,", ".join(self.FIELDS))
        name = self._convertFieldName(name)
        self.data[name]=value
        setattr(self,name,value)
        
    def __getitem__(self,name):
        """return an item from the dict"""
        if name not in self.FIELDS:
            raise KeyError, "'%s' is not a valid key, please use one of %s" %(name,", ".join(self.FIELDS))
        name = self._convertFieldName(name)
        return self.data.get(name,u'')
        
    def setAdrAttribute(self,name,value):
        if name not in self.ADR_FIELDS:
            raise KeyError, "'%s' is not a valid adr key, please use one of %s" %(name,", ".join(self.FIELDS))
        name = self._convertFieldName(name)
        self.adr[name] = value
            
    def getAdrAttribute(self,name):
        """return an item from the dict"""
        if name not in self.ADR_FIELDS:
            raise KeyError, "'%s' is not a valid adr key, please use one of %s" %(name,", ".join(self.FIELDS))
        name = self._convertFieldName(name)
        return self.adr.get(name,u'')

    def __str__(self):
        """return a texture representation"""
        l=[]
        for field in self.FIELDS:
            if self[field]!=u'':
                l.append("%s=%s" %(field,self[field]))
        al = []
        for field in self.ADR_FIELDS:
            if self.adr[field]!=u'':
                al.append("%s=%s" %(field,self.adr[field]))
        
        return """VCard for %s (%s), Adr: %s""" %(self.data['fn'],", ".join(l),", ".join(al))
        
